package com.mdxl.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by pangzhuanxia on 2017/5/11.
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"
    };

    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, Object... pattern) {
        String formatDate = null;
        if (pattern != null && pattern.length > 0) {
            formatDate = DateFormatUtils.format(date, pattern[0].toString());
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }

    /**
     * 时间戳转为日期 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
//    public static String stampToDateTime(Integer s){
//        String res;
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date date=new Date(s);
//        res=simpleDateFormat.format(date);
//        return res;
//    }
    public static String stampToDateTime(String s) {
        Long lt = Long.parseLong(s);
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");


        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToDateTime1(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String res;
        Long time = new Long(s);
        Date date = new Date(time);
        res = simpleDateFormat.format(date);

// Date date=format.parse(d);
//        System.out.println("Format To String(Date):"+d);
//        System.out.println("Format To Date:"+date);
        return res;
    }

        /*
    时长转化为秒,time格式 hh:mm:ss
     */
    public static Long timeLenthToSecond(String time) throws Exception {
        String[] times = time.split(":");
        if (times.length != 3) {
            throw new Exception("time not valid");
        }
        Long seconds = 0L;

        seconds = Long.parseLong(times[0]) * 60 * 60;
        seconds+=Long.parseLong(times[1])*60;
        seconds+=Long.parseLong(times[2]);
        return seconds;
    }

    public static String diffTime(Date date1, Date date2){
        long ms=date2.getTime()-date1.getTime();//除以1000是为了转换成秒
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
//        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分");
        }
        if(second > 0) {
            sb.append(second+"秒");
        }
//        if(milliSecond > 0) {
//            sb.append(milliSecond+"毫秒");
//        }
        return sb.toString();
    }
}
